package com.example.downlinetext;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.Layout;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;

public class UnderLineTextView extends androidx.appcompat.widget.AppCompatTextView {
    private Rect mRect;
    private Paint mPaint;
    private Paint mPaint2;
    private int mColor;
    private int mColor2;
    private float density;
    private float mStrokeWidth;
    private String[] Positions={"down","ParentOfDown"};
    private String Position = Positions[0];
    StringBuilder sb = new StringBuilder();
    private Integer UnderLineNumPosition=0;// 据字体的距离
    private Boolean isInt=true;


    public UnderLineTextView(Context context) {
        this(context, null, 0);
    }
    public UnderLineTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public UnderLineTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr){
        //获取屏幕密度
        density=context.getResources().getDisplayMetrics().density;
        //获取自定义属性
        TypedArray array=context.obtainStyledAttributes(attrs,R.styleable.UnderlinedTextView,defStyleAttr,0);
        mColor=array.getColor(R.styleable.UnderlinedTextView_underlineColor,0xFFFF0000);
        mColor2=array.getColor(R.styleable.UnderlinedTextView_underlineColor2,0xFFFF0000);
        mStrokeWidth=array.getDimension(R.styleable.UnderlinedTextView_underlineWidth,density*2);
        Position = array.getString(R.styleable.UnderlinedTextView_underLinePosition);
        UnderLineNumPosition = array.getInteger(R.styleable.UnderlinedTextView_underLineNumPosition,0);
        array.recycle();

        mRect=new Rect();
        mPaint =new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(mStrokeWidth);
        mPaint2 =new Paint();
        mPaint2.setStyle(Paint.Style.STROKE);
        mPaint2.setColor(mColor2);
        mPaint2.setStrokeWidth(mStrokeWidth);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        //得到TextView显示有多少行
        if(!TextUtils.isEmpty(getText())){
            int count=getLineCount();//一共有几行

            //得到TextView的布局
            final Layout layout=getLayout();

            float x_start,x_stop,x_diff;
            int firstCharInLine, lastCharInLine;
            float underLinePosition;

            for (int i = 0; i < count; i++) {
                int baseline=getLineBounds(i,mRect);
                firstCharInLine=layout.getLineStart(i);// 获取第i行第一个字符
                lastCharInLine = layout.getLineEnd(i);// 获取第i行最后一个字符
                int y=i+1;
                Log.d("TAG","第"+y+"行："+"开始字符串位置:"+firstCharInLine+"\n"+"最后一个字符："+lastCharInLine);
//                包括第一个，不包括第二个 firstCharInLine：0 lastCharInLine：8 实则计算到7，第八个是下一行的第一个
//                getPrimaryHorizontal()
//                layout.getSecondaryHorizontal(int a) a为下一行的第一个
                x_start = layout.getPrimaryHorizontal(firstCharInLine);
                x_diff =layout.getPrimaryHorizontal(firstCharInLine+1)-x_start;
                x_stop = layout.getPrimaryHorizontal(lastCharInLine-1)+x_diff;
                if(isInt){
                    x_stop = layout.getPrimaryHorizontal(lastCharInLine-1);
                }

                if(i == count-1){
                    x_stop = layout.getSecondaryHorizontal(lastCharInLine);
                }

                underLinePosition = baseline;
                if(TextUtils.isEmpty(Position)){
                    underLinePosition=baseline+mStrokeWidth;
                }else if(Position.equals(Positions[1])){
                    underLinePosition =baseline ;
                }
                if(!UnderLineNumPosition.equals(0)){
                    underLinePosition =baseline+UnderLineNumPosition;
                }

                switch (i){
                    case 1:canvas.drawLine(x_start,underLinePosition,x_stop, underLinePosition, mPaint2);
                        break;
                    default:canvas.drawLine(x_start,underLinePosition,x_stop, underLinePosition, mPaint);
                        break;
                }

            }
        }

        super.onDraw(canvas);

    }
    private float pxToDp(float px){
        return px/density+0.5f;
    }
    private float dpToPx(float dp){
        return dp*density+0.5f;
    }

    public int getUnderLineColor() {
        return mColor;
    }

    public void setUnderLineColor(int mColor) {
        this.mColor = mColor;
        invalidate();
    }
    public void getWordText(getWord getWord){
        getWord.Word(sb.toString());
    }

    public float getUnderlineWidth() {
        return mStrokeWidth;
    }
    public void setUnderlineNumPosition(Integer p) {
        this.UnderLineNumPosition = p;
        invalidate();
    }

    public void setUnderlineWidth(float mStrokeWidth) {
        this.mStrokeWidth = mStrokeWidth;
        invalidate();
    }
}
